<?php


class dbtoclass{
    var $db;
    var $updated_vars;
    var $id;    

    
    function makeGetSetMethods(){
       foreach(get_object_vars($this) as $k => $v){
           echo "\n\t function set".ucfirst($k)."(\$var){\r\t\t
           ".'$this->'.$k.' = $var;
           '."\r}";
           #echo "\n";
           echo "\n\t function get".ucfirst($k)."(){\r\t\t
           return ".'$this->'.$k.";
           \r}";
           echo "\n";echo "\n\n";
       }
    }
    
   
     /**
      * Set hash-array of updated property name & new updated property value.
      *
      * @param unknown_type $arr
      */
     function setUpdatedVars($arr = array()){
         $this_arr = $this->updated_vars;
         if(!is_array($this->updated_vars)){
             $this_arr = $arr;
         }
         else{
             foreach ($arr as $key => $val){
                $this_arr[$key] = $val;
             }
         }
         $this->updated_vars = $this_arr;
     }
     
     
    function checkout($id = false){
       if($id == false or !is_numeric($id)){return false;}
       $this->setId($id);
       $db = $this->getDb();
       $data = $db->getRow("select * from ".$this->dbtable." where ".$this->pkeyfld." = '".$this->getId()."'");
       
       foreach ($data as $kk => $vv){
           if(in_array(strtolower($kk),array_keys(get_object_vars($this)))){
               foreach(get_object_vars($this) as $k => $v){
                   $set_m = 'set'.ucfirst($k);
                   #$get_m = 'get'.ucfirst($k);
                   if(strtolower($kk) == strtolower($k)){
                       $this->$set_m($vv);
                   }
               }
           }
       }
        
       return true;
    }
    
    function setNewValues($hash = false){
        if($hash == false or !is_array($hash)){return false;}
        foreach ($hash as $post_k => $post_v){
            if(in_array(strtolower($post_k),array_keys(get_object_vars($this)))){
               foreach(get_object_vars($this) as $this_k => $this_v){
                   if(strtolower($this_k) == $post_k){
                       $set_m = 'set'.ucfirst($this_k);
                       $get_m = 'get'.ucfirst($this_k);
    
                       $now_obj_value = trim($this->$get_m());
                        
                       if($now_obj_value != $post_v){
                           #Log updated values. >>>
                           $update[$post_k] = $post_v;
                           #<<<
                           #Update class values. >>>
                           $this->$set_m($post_v);
                           #<<<
                        }
                    }                
                }
            }
        }

        $this->setUpdatedVars($update);
           
        return $update;
    }
    
     function makeUpdateSqlString($hash = array()){
        
        $set = '';
        if(!is_array($hash)){return '';}

        foreach ($hash as $k => $v) {
                $set .= ($set == "" ? "" : ", ")."`{$k}` = '".addslashes(trim(($v)))."'";
        }
        
        return $set;
       
     }
     
    function setId($var){
    
       $this->id = $var;
       
    }
    function getId(){
    
       return $this->id;
       
    }         

    /**
     * Commit changes to DB and write changes to history_log table.
     *
     */
    function commit(){

        $updateitemsql = false;
        $allset = $this->makeUpdateSqlString($this->getUpdatedVars());
        $db = $this->db;
        if($allset != '' && strlen($allset) > 2){
            $updateitemsql = 'update '.$this->dbtable.' set '.$allset.' where '.$this->pkeyfld."  =  '".$this->getId()."'";
            #echo "<br /> $updateitemsql";
            $db->Exec($updateitemsql);
        }
        
        $this->writeHistoryLog();
        return true;
        
    }

    function writeHistoryLog(){
        #dummy interface
        return true;
    }
     
     function getUpdatedVars(){
         return $this->updated_vars;
     }

    function setDb($var){
    
       $this->db = $var;
       
    }
    function getDb(){
    
       return $this->db;
       
    }

    /**
     * Create new record with given properties & values
     *
     */
    function create($hash = array()){
        if(count($hash) > 0){
            $db = $this->getDb();
            $db->exec('insert into '.$this->dbtable." (`".$this->pkeyfld."`) value ('')");
            $id = $db->getInsertID();
            
            $this->checkout($id);
            $this->setNewValues($hash);
            $this->commit();
            return $id;
        }
        else{
            return false;
        }
    }
    
    function delete($id = false){
        if($id == false or !is_numeric($id)){return false;}
        $db = $this->getDb();
        $db->exec('delete from '.$this->dbtable." where ".$this->pkeyfld." = '".$id."'");
        return true;
        
    }

    function finish(){
        return true; #dummy interface
    }   
     
}


?>